package com.wan.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Description: Redis配置类
 * date: 2021/12/23 12:04
 *
 * @author 万富
 * @since JDK 1.8
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        // String 类型 key序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // String 类型 value序列化器
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        // hash 类型 key序列化器
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        // hash 类型 key序列化器
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        // 放入连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
}
